package com.jingdianjichi.subject.domain.handler.Subject;

import com.jingdianjichi.subject.common.enums.SubjectTypeEnums;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**'
 * 题目类型工厂
 */
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {
    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlers;

    private Map<SubjectTypeEnums,SubjectTypeHandler> handlerMap=new HashMap<>();

    public SubjectTypeHandler getHandler(int subjectType) {
        SubjectTypeEnums subjectTypeEnum=SubjectTypeEnums.getResultEnumCode(subjectType);
        return handlerMap.get(subjectTypeEnum);
    }

    @Override  //这里Bean实例化后进行装载这种方式便于题型后续的扩展
    public void afterPropertiesSet() throws Exception {
        subjectTypeHandlers.forEach(
                subjectTypeHandler ->
                { handlerMap.put(subjectTypeHandler.getSubjectType(),subjectTypeHandler); });
    }
}
